home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13808 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.5 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Recursion
  5. Date: Wed, 10 Apr 96 11:54:12 GMT
  6. Organization: none
  7. Message-ID: <829137252snz@genesis.demon.co.uk>
  8. References: <31624BC2.70D2@sooner.net> <828548265snz@genesis.demon.co.uk> <4k10q0$m5d@linet06.li.net> <4k4n40$tbi@news.microsoft.com>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4k4n40$tbi@news.microsoft.com>
  15.            a-cnadc@microsoft.com "Dann Corbit" writes:
  16.  
  17. >In article <4k10q0$m5d@linet06.li.net>, jeremy@newshost.li.net says...
  18. >>
  19. >>Lawrence Kirby (fred@genesis.demon.co.uk) wrote:
  20. >>
  21. >>: I think you've picked a bad example. You really need an extra argument in
  22. >>: this case to pass on a running accumulator. A more suitable problem would
  23. >>: be to write a recursive function that is passed an integer (unsigned is
  24. >>: easiest) and writes the character representation to stdout.
  25. >>Absolutely WRONG.  The whole point of recursion is that you do not need a 
  26. >>running accumulator.  When written correctly, the return value of each 
  27. >>recursive loop will be the correct accumulated value.  ANyhow, here is a 
  28. >>better version of what I already posted...
  29. >>
  30. >>#include <stdio.h>
  31. >>#include <stdlib.h>
  32. >>#include <string.h>
  33. >>#include <math.h>
  34. >>
  35. >>int convert(char *string)
  36. >> {
  37. >>  int value;
  38. >>  int len = strlen(string);
  39. >>
  40. >>  if (string[0] == '\0')
  41. >>    return(0);
  42. >>  value = convert(string+1);
  43. >>  value += ((string[0] - '0') * pow(10,len-1));
  44. >>  return(value);
  45. >> }
  46. >>
  47. >>void main()
  48. >> {
  49. >>  char *string = "1234";
  50. >>
  51. >>  printf("%d\n",convert(string));
  52. >> }
  53. >
  54. >I have to agree, with Mr. Kirby that recursion is a most
  55. >inelegant way to do this.  Here is another ugly recursive
  56. >solution.  The elegant way is atoi().  Iterative is next
  57. >best.  Recursive is downright silly.  But we all have our
  58. >silly moments.
  59.  
  60. I'm not saying that recursion is inelegant. I'm saying that the elegant
  61. way to solve this problem recursively is to use an accumulator. IMHO
  62. Bill's is the most elegant solution posted so far (and uses an accumuator),
  63. and not just because it is short. Elegance is one of recursion's great
  64. strengths (when it is done right), however its practicality (at least in C)
  65. is weaker.
  66.  
  67. ...
  68.  
  69. >void main( )
  70.  
  71. There's no need to copy bad code! :-)
  72.  
  73. -- 
  74. -----------------------------------------
  75. Lawrence Kirby | fred@genesis.demon.co.uk
  76. Wilts, England | 70734.126@compuserve.com
  77. -----------------------------------------
  78.